/**
 * Copyright (c) 2010-2011 julewa.com.  
 * All rights reserved. 
 * 
 * @author Huang Benhua
 * @date 2011-2-20
 * 
**/
package com.julewa;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

@SuppressWarnings("serial")
public class ThreadValue<T>{
	protected static final Set<ThreadValue<?>> THREADS = new HashSet<ThreadValue<?>>(){
		public boolean remove(Object obj){return false;};
		public boolean removeAll(Collection<?> c){return false;};
		public void clear(){};
	};
   
	private ThreadLocal<T> t;
    private T value;
    public ThreadValue(){
    	THREADS.add(this);
        t = new ThreadLocal<T>();
    }
    
    public void initialize(){
        t.set(null);
    }
    
    public T get(){
    	return t.get();
    }
    
    public T get(T defaultvalue){
    	value = t.get();
    	return value == null?defaultvalue:value;
    }
    
    public void set(T t){
    	this.t.set(t);
    }
}
